package com.dragon.common.modules.file.service.assistants;

import com.chris.dev.base.libs.v1.http.results.ResourceResult;
import com.dragon.common.base.exceptions.CommonException;
import com.dragon.common.modules.file.common.config.ConditionalOnFileStrategy;
import com.dragon.common.spring.annotations.Assistant;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Chris Chan
 * Create On 2025/8/30 上午7:44
 * Use for:
 * Explain:
 */
@Assistant
@ConditionalOnFileStrategy("seaweedfs")
public class SeaweedFSAssistant {
    @Autowired
    SeaweedFSClient seaweedFSClient;

    /**
     * 上传字节数组
     *
     * @param fileName
     * @param bytes
     * @return
     */
    public ResourceResult uploadBytes(String fileName, byte[] bytes) {
        try {
            // 1. 健康检查
            if (!seaweedFSClient.healthCheck()) {
                throw CommonException.of("seaweedfs服务不可用");
            }
            // 2. 上传本地文件
            String remotePath = seaweedFSClient.getRemotePath(fileName);
            seaweedFSClient.uploadBytes(bytes, remotePath);
            String url = seaweedFSClient.getFileUrl(remotePath);
            return ResourceResult.create(url, remotePath);
        } catch (Exception e) {
            throw CommonException.of("seaweedfs文件上传失败");
        }
    }
}
